package com.lxl.plugin.packagejar.pack.impl;

import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.compiler.CompileContext;
import com.intellij.openapi.compiler.CompileStatusNotification;
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.lxl.plugin.packagejar.message.Messages;
import com.lxl.plugin.packagejar.pack.Packager;
import com.lxl.plugin.packagejar.util.Util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.HashSet;

/**
 * Created by LXLong on 2015/2/28.
 */
public class EachPacker implements Packager, CompileStatusNotification {

    private DataContext dataContext;
    private String exportPath;

    public EachPacker(DataContext dataContext, String exportPath) {
        this.dataContext = dataContext;
        this.exportPath = exportPath;
    }

    @Override
    public void pack() {
        Project project = CommonDataKeys.PROJECT.getData(dataContext);
        Module module = LangDataKeys.MODULE.getData(dataContext);

        VirtualFile[] virtualFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
        String outPutPath = CompilerPathsEx.getModuleOutputPath(module, false);

        HashSet<VirtualFile> directories = new HashSet<VirtualFile>();
        for (VirtualFile virtualFile : virtualFiles) {
            Util.iterateDirectory(project, directories, virtualFile);
        }

        String jdkPath = Util.getJDKPath(project);

        for (VirtualFile directory : directories) {
            PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(directory);
            if (psiDirectory != null) {
                PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
                StringBuilder command = new StringBuilder(jdkPath);
                command.append("jar");
                command.append(" cvf ");
                command.append(this.exportPath);
                command.append("/");
                command.append(psiPackage.getQualifiedName());
                command.append(".jar");
                File outPutDirectory = new File(outPutPath + "/" + psiPackage.getQualifiedName().replaceAll("\\.", "/"));
                if (outPutDirectory.exists()) {
                    for (File file : outPutDirectory.listFiles()) {
                        if (file.isFile()) {
                            command.append(" -C ");
                            command.append(outPutPath);
                            command.append(" ");
                            command.append(psiPackage.getQualifiedName().replaceAll("\\.", "/"));
                            command.append("/");
                            command.append(file.getName());
                        }
                    }
                }
                Messages.info(project, command.toString());
                Process process;
                try {
                    process = Runtime.getRuntime().exec(command.toString());
                    BufferedReader stream = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String str;
                    while ((str = stream.readLine()) != null) {
                        Messages.info(project, str);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void finished(boolean b, int error, int i1, CompileContext compileContext) {
        if (error == 0) {
            pack();
        } else {
            Project project = CommonDataKeys.PROJECT.getData(dataContext);
            Messages.info(project, "compile error");
        }
    }
}
